package org.example.review.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.common.pojo.entity.user;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.example.review.dto.UserDTO;

/**
 * 用户数据访问层接口
 */
@Mapper
public interface UserMapper extends BaseMapper<user> {
    
    /**
     * 根据用户名查询用户信息（用于登录）
     * @param username 用户名
     * @return 用户信息
     */
    @Select("SELECT user_id, username, department, role, auth_type, status, email, phone, " +
            "hire_date, password, create_time, update_time, is_deleted " +
            "FROM travel_user " +
            "WHERE username = #{username} AND is_deleted = 0")
    UserDTO selectByUsername(@Param("username") String username);
    
    /**
     * 根据用户ID查询用户信息
     * @param userId 用户ID
     * @return 用户信息
     */
    @Select("SELECT user_id, username, department, role, auth_type, status, email, phone, " +
            "hire_date, password, create_time, update_time, is_deleted " +
            "FROM travel_user " +
            "WHERE user_id = #{userId} AND is_deleted = 0")
    UserDTO selectByUserId(@Param("userId") Integer userId);
    
    /**
     * 根据用户ID更新密码
     * @param userId 用户ID
     * @param newPassword 新密码
     * @return 更新的记录数
     */
    @Update("UPDATE travel_user SET password = #{newPassword}, update_time = NOW() " +
            "WHERE user_id = #{userId} AND is_deleted = 0")
    int updatePasswordById(@Param("userId") Integer userId, @Param("newPassword") String newPassword);
    
    /**
     * 根据用户名更新密码
     * @param username 用户名
     * @param newPassword 新密码
     * @return 更新的记录数
     */
    @Update("UPDATE travel_user SET password = #{newPassword}, update_time = NOW() " +
            "WHERE username = #{username} AND is_deleted = 0")
    int updatePasswordByUsername(@Param("username") String username, @Param("newPassword") String newPassword);
}
